﻿using System;
using fastCSharp;
using System.Reflection;

namespace diantou.web
{
    /// <summary>
    /// 用户评论关系
    /// </summary>
    /// <typeparam name="commentType"></typeparam>
    /// <typeparam name="valueType"></typeparam>
    [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.userComment")]
    struct userComment<commentType, valueType, memberCacheType>
        where commentType : diantou.dataAccess.comment<commentType, valueType, memberCacheType>
        where valueType : class
        where memberCacheType : class
    {
        /// <summary>
        /// 评论
        /// </summary>
        public commentType Comment;
        /// <summary>
        /// 评论标识
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public int Id
        {
            get { return Comment == null ? 0 : Comment.Id; }
        }
        /// <summary>
        /// 用户标识
        /// </summary>
        public int UserId;
        /// <summary>
        /// 是否自己的评论
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public bool IsSelf
        {
            get { return Comment != null && Comment.UserId == UserId; }
        }
        /// <summary>
        /// 是否赞
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        public bool IsAgree
        {
            get { return UserId != 0 && Comment != null && Comment.UserId != UserId && getIsAgree(UserId, Comment.Id, dataModel.focus.type.Agree); }
        }

        /// <summary>
        /// 获取是否赞同委托
        /// </summary>
        private static readonly Func<int, int, diantou.dataModel.focus.type, fastCSharp.net.returnValue<bool>> getIsAgree;
        static userComment()
        {
            Type type = typeof(commentType);
            type = type.Assembly.GetType("diantou.dataAccess.tcpCall+focus" + type.Name[0].ToString().ToUpper() + type.Name.Substring(1));
            getIsAgree = (Func<int, int, diantou.dataModel.focus.type, fastCSharp.net.returnValue<bool>>)Delegate.CreateDelegate(typeof(Func<int, int, diantou.dataModel.focus.type, fastCSharp.net.returnValue<bool>>), type.GetMethod("IsFocus", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(int), typeof(int), typeof(diantou.dataModel.focus.type) }, null));
        }
    }
}
